﻿@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>商户定义</title>
    <link rel="stylesheet" type="text/css" href="~/Content/jquery-easyui-1.4.2/themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="~/Content/jquery-easyui-1.4.2/themes/icon.css">
    <link rel="stylesheet" type="text/css" href="~/Content/jquery-easyui-1.4.2/demo.css">
    <link href="~/Content/Styles/Manage_Order.css" rel="stylesheet" />
    <script type="text/javascript" src="~/Content/jquery-easyui-1.4.2/jquery.min.js"></script>
    <script type="text/javascript" src="~/Content/jquery-easyui-1.4.2/jquery.easyui.min.js"></script>
    <script src="~/Content/jquery-easyui-1.4.2/locale/easyui-lang-zh_CN.js"></script>

    <script>
        $(function () {  
            //initTable();
            getAdderss();
            mertype();
            MerDep(); 
            //MerID();
            $("#xiugai").linkbutton('disable');
            $("#shanc").linkbutton('disable');
        });
          
        //初始化表格  
        function initTable() {
            $("#xiugai").linkbutton('enable');
            $("#shanc").linkbutton('enable');
            $('#load').datagrid({
                iconCls: 'icon-user',
                loadMsg: '数据加载中...',
                nowrap: true, 
                autoRowHeight: true,
                striped: true,
                url: '/MerDefinition/GetAllAccount',
                sortName: 'MerID',
                sortOrder: 'asc',
                border: true,
                remoteSort: false,
                idField: 'MerID',
                pageSize: 10,
                singleSelect: true,
                pagination: true,
                rownumbers: true,
                columns: [[
					{ field: 'MerID', title: '商户编号', width: 100, sortable: true, align: 'center' },
					{ field: 'MerName', title: '商户名称', width: 250, sortable: true, align: 'center' },
                    { field: 'DelType', title: "商户类型ID", width: 100, align: 'center', hidden: true },
                    { field: 'DictName', title: "商户类型", width: 100, sortable: true, align: 'center', },
                    { field: 'MerAdr', title: "商户地址", width: 250, sortable: true, align: 'center' },
                    { field: 'MerCall', title: "商户联系电话", width: 150, sortable: true, align: 'center' },
                    {field: 'MerTxt', title: '商户介绍', width: 400, sortable: true, align: 'center'}
                ]],
                toolbar: '#tb'
            });
        }

        //商户类型
        function mertype() {
            //alert("商户类型");
            $.get("/MerDefinition/getmertype", {}, function (database) {
                console.log(database);
                $('#DelType').combobox({
                    valueField: 'DictKey',
                    textField: 'DictName',
                    value: '-1',
                    text: '全部',
                    data: database
                });
                $('#updatedeltypes').combobox({
                    valueField: 'DictKey',
                    textField: 'DictName',
                    data: database
                });
                $('#deltypes').combobox({
                    valueField: 'DictKey',
                    textField: 'DictName',
                    data: database
                });
            })
        }

        //地址集合
        function getAdderss() {
            //alert("商户类型");
            $.get("/MerDefinition/getAdderss", {}, function (database) {
                console.log(database);
                $('#a_address').combobox({
                    valueField: 'AddId',
                    textField: 'AddName',
                    data: database,
                    multiple: true,
                    editable: false,
                });
            })
        }

        //添加商户对话框
        function AddMer() {
            //alert("打开新增框");
            $('#AddMerDialog').dialog('open').dialog('setTitle', '新增商户');
            Clear(); 
        }


        //添加商户添加事件
        function BindAddMerClickEvent() {
                //验证所有的文本框是否通过用户的验证
                var valid = $('#ff').form('validate');
                if (valid == false) {
                    return false;
                }
               
                var phone = $("#mercall").textbox('getValue');
                var filter = /^((0\d{2,3})-)?(\d{7,8})(-(\d{3,}))?$/;//固话
                var sMobile = /^1[3|4|5|8][0-9]\d{4,8}$/;//手机号

                var lastchanmon = "1@0";
                var overchanmon = "1@0";
                var postdata = {
                    LastChanMon: lastchanmon,
                    OverChanMon: overchanmon,
                    MerState:1,
                    MerName: $("#mername").textbox('getValue'),
                    MerAdr: $("#meradr").textbox('getValue'),
                    DelType: $("#deltypes").combobox('getValue'),
                    MerCall: $("#mercall").textbox('getValue'),
                    MerDep: $("#merdep").combotree('getValue'),
                    MerTxt: $("#mertxt").textbox('getValue'),
                    //addersss: $("#a_address").combotree('getValues'),
                };
            //判断是否为有效号码
                if (sMobile.test(phone) || filter.test(phone)) {
                    //发送异步请求到后台保存用户数据
                    $.post("/MerDefinition/addMer", postdata, function (data) {
                        if (data == "OK") {
                            $.messager.alert("友情提示", "添加成功");
                            Clear();
                            $('#AddMerDialog').dialog('close');
                            $("#load").datagrid("reload");
                        }
                        else {
                            $.messager.alert("友情提示", "添加失败，请您检查");
                        }
                    });
                }
                else {
                    $.messager.alert("友情提示", "请填完整的11位手机号或者正确的固定电话号!");
                }
            //});
        }

        //清空文本框中的值
        function Clear() {
            $("#mername").textbox('setValue', "");   //绑定商户名称
            $("#meradr").textbox('setValue', "");     //绑定商户地址
            $("#deltypes").combobox('setValue', ""); //绑定商户类型
            $("#mercall").textbox('setValue', "");   //绑定商户电话
            $("#merdep").combotree('setValue', "");     //绑定消费范围
            $("#mertxt").textbox('setValue', "");
            $("#a_address").combobox('setValue', "")
        }

       // 验证电话号码
        function Phone() {
            var phone = $("#mercall").textbox('getValue');
            var filter = /^((0\d{2,3})-)?(\d{7,8})(-(\d{3,}))?$/;//固话
            var sMobile = /^1[3|4|5|8][0-9]\d{4,8}$/;//手机号
            if (!(sMobile.test(phone)) | !(filter.test(phone))) {
                document.mobileform.mobile.focus();
                return false;
            }
        }

        //消费范围--增加
        function MerDep() {
            //alert("消费范围");
            $('#merdep').combotree({
                url: "/MerDefinition/getdep",
                method: 'get',
                multiple: true,
            });

            $('#updatemerdep').combotree({
                url: "/MerDefinition/getdep",
                method: 'get',
                multiple: true,
            });
        }
        //
        var MerID;//商户ID
        var MerPic;//商户图片
        var MerSort;
        var IsRet;
        var LastChanMon;
        var OverChanMon;
        var DelMaxMon;
        var DelFormMon;
        var DelTime;
        var MerState;
        var SalesCount;
        var EvaCount;
        var StarCount;
        var IsCashPay;

        //修改商户信息窗口
        function UpdateMer() {
            var UpdateID = $("#load").datagrid("getSelections");
            if (UpdateID.length == 1) {
                $("#UpdateMerDialog").dialog("open").dialog("setTitle", "修改商户定义");
                BingDetails();

            }
            else {
                $.messager.alert("友情提示", "每次只能修改一行数据，你已经选择了<font color='red' size='6'>" + UpdateID.length + "</font>行");
            }
        }

        //获取用户信息绑定
        function BingDetails() {
            var datas = $("#load").datagrid("getSelections")[0]; //获取选中行数据
            $("#updatemername").textbox('setValue', datas.MerName);   //绑定商户名称
            $("#updatemeradr").textbox('setValue', datas.MerAdr);     //绑定商户地址
            $("#updatedeltypes").combobox('setValue', datas.DelType); //绑定商户类型
            $("#updatemercall").textbox('setValue', datas.MerCall);   //绑定商户电话
            $("#updatemerdep").combotree('setValue', datas.MerDep);     //绑定消费范围
            $("#updatemertxt").textbox('setValue', datas.MerTxt);     //绑定商户介绍
            //$("#e_address").combobox('setValue', datas.DelType); //送餐地址范围

            $.get("/MerDefinition/getAdderss", {}, function (database) {
                console.log(datas.MerID);
                var adderID = []; //商户配送范围数组
                $.post("/MerDefinition/getMerAdderss?MerID=" + datas.MerID, function (resp) {
                    console.log(resp);

                    for (var i in resp) {
                        adderID.push(resp[i].MerSendAddId)
                    }
                    console.log("shu" + adderID);
                    $('#e_address').combobox({
                        valueField: 'AddId',
                        textField: 'AddName',
                        data: database,
                        value: adderID,
                        multiple: true,
                        editable: false,
                    });
                })

            })
        }

        //修改用户绑定事件
        function BindUpdateMerClickEvent() {
                //首先判断前台的验证是否通过
                var valid = $('#updateController').form('validate');
                MerID = $("#load").datagrid("getSelections")[0].MerID;    //商户ID
                MerPic = $("#load").datagrid("getSelections")[0].MerPic;  //商户图片
                MerSort = $("#load").datagrid("getSelections")[0].MerSort;  //排序
                IsRet = $("#load").datagrid("getSelections")[0].IsRet;  //是否允许退订
                LastChanMon = $("#load").datagrid("getSelections")[0].LastChanMon;  //默认商品在允许退订时间内进行退订，收取的费用比例
                OverChanMon = $("#load").datagrid("getSelections")[0].OverChanMon;  //默认商品超出退订进行退订，收取的费用比例
                DelMaxMon = $("#load").datagrid("getSelections")[0].DelMaxMon;  // 配送起步价
                DelFormMon = $("#load").datagrid("getSelections")[0].DelFormMon;  // 配送费用
                DelTime = $("#load").datagrid("getSelections")[0].DelTime; //预计配送耗时
                MerState = $("#load").datagrid("getSelections")[0].MerState;  //有效性
                SalesCount = $("#load").datagrid("getSelections")[0].SalesCount; //总销量数
                EvaCount = $("#load").datagrid("getSelections")[0].EvaCount; //总评论数
                StarCount = $("#load").datagrid("getSelections")[0].StarCount; //总星星数
                IsCashPay = $("#load").datagrid("getSelections")[0].IsCashPay;  //是否现金消费
                if (valid == false) {
                    return false;
                }
                //创建传递的参数
                var postdata = {
                    MerID: MerID,
                    MerPic:MerPic,
                    MerSort:MerSort,
                    IsRet:IsRet,
                    LastChanMon:LastChanMon,
                    OverChanMon:OverChanMon,
                    DelMaxMon:DelMaxMon,
                    DelFormMon:DelFormMon,
                    DelTime:DelTime,
                    MerState:MerState,
                    SalesCount:SalesCount,
                    EvaCount:EvaCount,
                    StarCount:StarCount,
                    IsCashPay:IsCashPay,
                    MerName: $("#updatemername").textbox('getValue'),
                    MerAdr: $("#updatemeradr").textbox('getValue'),
                    DelType: $("#updatedeltypes").combobox('getValue'),
                    MerCall: $("#updatemercall").textbox('getValue'),
                    MerDep: $("#updatemerdep").combotree('getValue'),
                    MerTxt: $("#updatemertxt").textbox('getValue'),
                    address: $("#e_address").combobox('getValues'),
                    };

            //使用异步实现修改用户信息
                $.post("/MerDefinition/UpdateMer?addersss=" + postdata.address, postdata, function (date) {
                if (date == "OK") {
                    $.messager.alert("友情提示", "修改成功");
                    $("#UpdateMerDialog").dialog('close');
                    $("#load").datagrid('reload');
                }
                else {
                    $.messager.alert("友情提示","修改失败，请检查");
                }
            });
        //});
        }

        //删除商户
        function DeleteMer() {
            var checkdata = $("#load").datagrid("getSelections");//获取整行数据
            if (checkdata.length == 1) {
                //发送异步请求删除数据
                //alert(checkdata)
                $.messager.confirm("删除商户", "您确认删除<font color='Red' size='3'>" + checkdata[0].MerName + "</font>这个商户吗？", function (DeleteMer) {
                    if (DeleteMer) {
                        $.post("/MerDefinition/deletemer", checkdata[0], function (data) {
                            if (data == "OK") {
                                $.messager.alert("友情提示", "删除成功");
                                initTable();
                            } 
                            else {
                                $.messager.alert("友情提示", "删除失败:" + data);
                            }
                        });
                    }
                });
            }
            else {
                $.messager.alert("友情提示", "每次只能删除一行数据，你已经选择了<font color='red' size='6'>" + checkdata.length + "</font>行");
            }
        }

        //搜索
        function Search() {
            var DelType = $('#DelType').combobox('getValue');
            var MerName = $('#MerName').textbox('getValue');
            var MerAdr = $('#MerAdr').textbox('getValue');
            if (DelType != "" || MerName != "" || MerAdr != "") {
                $("#xiugai").linkbutton('enable');
                $("#shanc").linkbutton('enable');
                $('#load').datagrid({
                    iconCls: 'icon-user',
                    loadMsg: '数据加载中...',
                    nowrap: true,
                    autoRowHeight: true,
                    striped: true,
                    url: '/MerDefinition/SearchMerDefinition?DelType=' + DelType + "&MerName=" + MerName + "&MerAdr=" + MerAdr,
                    sortName: 'MerID',
                    sortOrder: 'asc',
                    border: true,
                    remoteSort: false,
                    idField: 'MerID',
                    pageSize: 10,
                    singleSelect: true,
                    pagination: true,
                    rownumbers: true,
                    columns: [[
                        { field: 'MerID', title: '商户编号', width: 100, sortable: true, align: 'center' },
                        { field: 'MerName', title: '商户名称', width: 250, sortable: true, align: 'center' },
                        { field: 'DelType', title: "商户类型ID", width: 100, align: 'center', hidden: true },
                        { field: 'DictName', title: "商户类型", width: 100, sortable: true, align: 'center', },
                        { field: 'MerAdr', title: "商户地址", width: 250, sortable: true, align: 'center' },
                        { field: 'MerCall', title: "商户联系电话", width: 150, sortable: true, align: 'center' },
                        { field: 'MerTxt', title: '商户介绍', width: 400, sortable: true, align: 'center' }
                    ]],
                    toolbar: '#tb'
                });
            }
        }

        //清空文本框中的值
        function ClearText() {

        }

        // 清除搜索框内容
        function away() {
            $('#DelType').combobox('setValue', "");
            $('#MerName').textbox('setValue', "");
            $('#MerAdr').textbox('setValue', "");
        }
    </script>

</head>
<body style="padding: 0;">
    <!--表格加载S-->
    <div>
        <table id="load" style="width: auto;"></table>
    </div>
    <!--表格加载E-->

    <!--工具栏S-->
    <div id="tb" style="padding: 5px; height: auto">
        <div style="margin-bottom: 5px">
            <a href="#" class="easyui-linkbutton" iconcls="icon-add" onclick="AddMer()">新增商户</a>&nbsp;
            <a href="#" id="xiugai" class="easyui-linkbutton" iconcls="icon-edit" onclick="UpdateMer()">修改商户</a>&nbsp;
            <a href="#" id="shanc" class="easyui-linkbutton" iconcls="icon-cut" onclick="DeleteMer()">删除商户</a>&nbsp;
            <a href="#" class="easyui-linkbutton" onclick="initTable(),away()" iconcls="icon-search">所有商户</a>
            <hr style="margin-top: 5px" />
            &nbsp;&nbsp;
            查询条件：&nbsp;
            商户类型:&nbsp;<input id="DelType" class="easyui-combobox" style="width: 100px"/>&nbsp;
            商户名称:&nbsp;<input id="MerName" class="easyui-textbox" style="width: 100px" />&nbsp;
            商户地址:&nbsp;<input id="MerAdr" class="easyui-textbox" style="width: 120px">&nbsp;
			<a href="#" class="easyui-linkbutton" onclick="Search()" iconcls="icon-search">查询</a>
        </div>
    </div>
    <!--工具栏E-->

    <!--新增S-->
    <div id="AddMerDialog" class="easyui-dialog" style="width: 550px; padding: 10px 0" closed="true" resizable="true" modal="true" buttons="#dlg-buttons" align="center">
        <form id="ff" method="post" novalidate="novalidate">
            <ul style="margin-bottom: 30px;">
                <li class="info-li" style="margin-top: 10px">
                    <div class="info-li-first fl">商户名称：</div>
                    <div class="info-li-second fl">
                        <input id="mername" class="easyui-textbox" style="width: 90%; height: 30px" data-options="required:true">
                    </div>
                </li>
                <li class="info-li">
                    <div class="info-li-first fl">商户地址：</div>
                    <div class="info-li-second fl">
                        <input id="meradr" class="easyui-textbox" style="width: 90%; height: 30px" data-options="required:true">
                    </div>
                </li>
                <li class="info-li">
                    <div class="info-li-first fl">商户类型：</div>
                    <div class="info-li-second fl">
                        <input id="deltypes" class="easyui-combobox" style="width: 90%; height: 30px;">
                    </div>
                </li>
                <li class="info-li">
                    <div class="info-li-first fl">联系电话：</div>
                    <div class="info-li-second fl">
                        <input id="mercall" class="easyui-textbox" style="width: 90%; height: 30px" data-options="required:true">
                    </div>
                </li>
                <li class="info-li">
                    <div class="info-li-first fl">允消费范围：</div>
                    <div class="info-li-second fl">
                        <input id="merdep" class="easyui-combotree" style="width: 90%; height: 30px">
                    </div>
                </li>
                @*<li class="info-li">
                    <div class="info-li-first fl">送餐范围：</div>
                    <div class="info-li-second fl">
                        <input id="a_address" class="easyui-combotree" style="width: 90%; height: 30px">
                    </div>
                </li>*@
                <li class="info-li">
                    <div class="info-li-first fl">商户介绍：</div>
                    <div class="info-li-second fl">
                        <input id="mertxt" class="easyui-textbox" data-options="multiline:true" style="width: 90%; height: 80px">
                    </div>
                </li>
                <li class="info-li">
                    <div class="info-li-first fl">&nbsp;</div>
                    <div class="info-li-second fl">
                        <a id="btnAddMer" href="#" onclick="BindAddMerClickEvent()" class="easyui-linkbutton" data-options="iconCls:'icon-add'">保存</a>
                        <a id="btnCancel" href="javascript:(0)" onclick="javascript:$('#AddMerDialog').dialog('close')" class="easyui-linkbutton" data-options="iconCls:'icon-calcel'">取消</a>
                    </div>
                </li>
            </ul>
        </form>
    </div>
    <!--新增E-->

    <!--修改S-->
    <div id="UpdateMerDialog" class="easyui-dialog" style="width: 550px; padding: 10px 0" closed="true" resizable="true" modal="true" buttons="#dlg-buttons" align="center">
        <form id="updateController" method="post" action="/MerDefinition/updatemerdefinition" novalidate="novalidate">
            <ul style="margin-bottom: 30px;">
                <li class="info-li" style="margin-top: 10px">
                    <div class="info-li-first fl">商户名称：</div>
                    <div class="info-li-second fl">
                        <input id="updatemername" class="easyui-textbox" style="width: 90%; height: 30px" data-options="required:true">
                    </div>
                </li>
                <li class="info-li">
                    <div class="info-li-first fl">商户地址：</div>
                    <div class="info-li-second fl">
                        <input id="updatemeradr" class="easyui-textbox" style="width: 90%; height: 30px" data-options="required:true">
                    </div>
                </li>
                <li class="info-li">
                    <div class="info-li-first fl">商户类型：</div>
                    <div class="info-li-second fl">
                        <input id="updatedeltypes" class="easyui-combobox" style="width: 90%; height: 30px;">
                    </div>
                </li>
                <li class="info-li">
                    <div class="info-li-first fl">联系电话：</div>
                    <div class="info-li-second fl">
                        <input id="updatemercall" class="easyui-textbox" style="width: 90%; height: 30px" data-options="required:true">
                    </div>
                </li>
                <li class="info-li">
                    <div class="info-li-first fl">允消费范围：</div>
                    <div class="info-li-second fl">
                        <input id="updatemerdep" class="easyui-combotree" style="width: 90%; height: 30px">
                    </div>
                </li>
                 <li class="info-li">
                    <div class="info-li-first fl">送餐范围：</div>
                    <div class="info-li-second fl">
                        <input id="e_address" class="easyui-combotree" style="width: 90%; height: 30px">
                    </div>
                </li>
                <li class="info-li">
                    <div class="info-li-first fl">商户介绍：</div>
                    <div class="info-li-second fl">
                        <input id="updatemertxt" class="easyui-textbox" data-options="multiline:true" style="width: 90%; height: 80px">
                    </div>
                </li>
                <li class="info-li">
                    <div class="info-li-first fl">&nbsp;</div>
                    <div class="info-li-second fl">
                        <a id="btnUpdateMer" href="javascript:(0)" onclick="BindUpdateMerClickEvent()" class="easyui-linkbutton" data-options="iconCls:'icon-add'">修改</a>
                        <a id="btnUpdateCancel" href="javascript:(0)" onclick="javascript:$('#UpdateMerDialog').dialog('close')" class="easyui-linkbutton" data-options="iconCls:'icon-calcel'">取消</a>
                    </div>
                </li>
            </ul>
        </form>
    </div>
    <!--修改E-->

</body>
</html>
